package cn.ithers.blog.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ithers.blog.dao.FriendLinkDao;
import cn.ithers.blog.dto.FriendLinkDTO;
import cn.ithers.blog.pojo.FriendLink;
import cn.ithers.blog.pojo.User;
import cn.ithers.blog.response.ResponseResult;
import cn.ithers.blog.service.IFriendLinkService;
import cn.ithers.blog.service.IUserService;
import cn.ithers.blog.utils.Constants;
import cn.ithers.blog.utils.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: zhanghuan
 * @date: 2020/9/29 9:30
 * @description:
 */
@Slf4j
@Service
public class FriendLinkServiceImpl extends BaseService implements IFriendLinkService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private FriendLinkDao friendLinkDao;

    @Autowired
    private IUserService userService;

    /**
     * 添加友情链接
     *
     * @param friendLinkDTO
     * @return
     */
    @Override
    public ResponseResult addFriendLink(FriendLinkDTO friendLinkDTO) {
        //判断数据
        String url = friendLinkDTO.getUrl();
        if (StrUtil.isEmpty(url)) {
            return ResponseResult.FAILED("链接不可为空");
        }
        String logo = friendLinkDTO.getLogo();
        if (StrUtil.isEmpty(logo)) {
            return ResponseResult.FAILED("logo不可为空");
        }
        String name = friendLinkDTO.getName();
        if (StrUtil.isEmpty(name)) {
            return ResponseResult.FAILED("对方名称不可为空");
        }
        //补全数据
        FriendLink friendLink = new FriendLink();
        friendLink.setUrl(url);
        friendLink.setLogo(logo);
        friendLink.setName(name);
        friendLink.setId(idWorker.nextId() + "");
        friendLink.setCreateTime(new Date());
        friendLink.setUpdateTime(new Date());
        //保存数据
        friendLinkDao.save(friendLink);
        //返回结果
        log.info("------------------>友情链接添加成功");
        return ResponseResult.SUCCESS("友情链接添加成功");
    }

    /**
     * 获取友情链接
     *
     * @param friendLinkId
     * @return
     */
    @Override
    public ResponseResult getFriendLink(String friendLinkId) {
        FriendLink friendLink = friendLinkDao.findOneById(friendLinkId);
        if (friendLink == null) {
            return ResponseResult.FAILED("该友链不存在");
        }
        return ResponseResult.SUCCESS("获取友链成功").setData(friendLink);
    }

    /**
     * 获取友情链接列表
     *
     * @return
     */
    @Override
    public ResponseResult getFriendLinkList() {
        //创建查询条件
        Sort sort =  Sort.by(Sort.Direction.DESC, "order", "createTime");
        User user = userService.checkUser();
        List<FriendLink> all;
        if (user == null || !Constants.User.ROLE_ADMIN.equals(user.getRoles())) {
            all = friendLinkDao.listFriendLinkByState("1");
        } else {
            all = friendLinkDao.findAll(sort);
        }
        return ResponseResult.SUCCESS("获取友情链接列表成功").setData(all);
    }

    /**
     * 删除友链
     *
     * @param friendLinkId
     * @return
     */
    @Override
    public ResponseResult deleteFriendLink(String friendLinkId) {
        int result = friendLinkDao.deleteAllById(friendLinkId);

        if (result == 0) {
            return ResponseResult.FAILED("该友情链接不存在");
        }

        return ResponseResult.SUCCESS("删除成功");
    }

    /**
     * 更新友链
     * 更新内容有什么：logo/名称/url/order/
     *
     * @param friendLinkId
     * @param friendLinkDTO
     * @return
     */
    @Override
    public ResponseResult updateFriendLink(String friendLinkId, FriendLinkDTO friendLinkDTO) {
        FriendLink friendLinkDb = friendLinkDao.findOneById(friendLinkId);
        if (friendLinkDb == null) {
            return ResponseResult.FAILED("该友情链接不存在");
        }
        String name = friendLinkDTO.getName();
        if (StrUtil.isEmpty(name)) {
            return ResponseResult.FAILED("友链名字不能为空");
        }
        String logo = friendLinkDTO.getLogo();

        if (StrUtil.isEmpty(logo)) {
            return ResponseResult.FAILED("友链logo不能为空");
        }
        String url = friendLinkDTO.getUrl();

        if (StrUtil.isEmpty(url)) {
            return ResponseResult.FAILED("友链url不能为空");
        }
        friendLinkDb.setName(name);
        friendLinkDb.setLogo(logo);
        friendLinkDb.setUrl(url);
        friendLinkDb.setOrder(friendLinkDTO.getOrder());
        friendLinkDb.setUpdateTime(new Date());
        //保存数据
        log.info("------------------->friendLinkDb");
        friendLinkDao.save(friendLinkDb);

        return ResponseResult.SUCCESS("更新友情链接成功");
    }
}
